#include "thread_pool.h"

// 线程任务函数(客户)
void *my_task(void* arg)
{
    int i = 0;
    char *p = (char*)arg;
    printf("地址:%p、内容:%s\n", p, p);
    while (1)
    {
        printf("正在执行 %p 任务 %d 次\n", p, i++);
        if(i == 60)
        {
            printf("任务结束\n");
            pthread_exit(NULL);
        }
        sleep(10);
    }
}



// 主函数
int main(int argc, char const *argv[])
{
    // 1、初始化线程池(公司创建完毕，一开始有5个员工)
    thread_pool *pool = malloc(sizeof(thread_pool));
    THREAD_POOL_Init(pool, 5);

    // 2、主线程
    int n = 0;
    while (1)
    {
        printf("1、增加线程(招聘员工)\n");
        printf("2、减少线程(解雇员工)\n");
        printf("3、任务添加(有客户来了)\n");
        printf("4、销毁线程池(解散公司)\n");

        scanf("%d", &n);
        while(getchar()!='\n');

        switch (n)
        {
            case 1:
                THREAD_POOL_AddThread(pool, 1);
                break;
            
            case 2:
                THREAD_POOL_RemoveThread(pool, 1);
                break;

            case 3:
                printf("请输入任务名称\n");
                char *name = malloc(1024);
                scanf("%s", name);
                printf("main:%p %s\n", name, name);
                THREAD_POOL_AddTask(pool, my_task, (void*)name);   // 传递给任务函数的地址、必须要存在
                break;

            case 4:
                goto exit_system; 
                break;
        }

    }
exit_system:
    THREAD_POOL_Destroy(pool);
    return 0;
}
